package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class connection extends Sprite
	{
		private static const SERVER_URL:String = "localhost";
		private static const PORT:int = 10002;
		
		private static var ip:int = 1;
		
		private static var _instance:connection;
		
		private var _socket:Socket;
		
		private static const checkNum:uint = 0;
		
		public function connection() 
		{
			var bytes:ByteArray = new ByteArray;
			bytes.writeByte(1);
			if (_instance)
			{
				throw new Error("Single");
			}
			initSocket();
			read();
			write(bytes);
			read();
		}
		
		public function initSocket():void
		{
			if (_socket == null)
			{
				_socket = new Socket(SERVER_URL, PORT);
				_socket.timeout = 2000;
				_socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
				_socket.addEventListener(ProgressEvent.SOCKET_DATA, onProgress);
			}
		}
		
		public function connectServer():void
		{
			if (_socket.connected == false)
			{
				_socket.connect(SERVER_URL, PORT);
			}
			else
			{
				trace("has connected the server");
			}
		}
		
		public function read():void
		{
			connectServer();
		}
		
		public function write(bytes:ByteArray):void
		{
			connectServer();
			_socket.writeUnsignedInt(ip++); //
			_socket.writeDouble(10.444);
			_socket.writeUnsignedInt(4355);
			if (bytes)
			{
				_socket.writeBytes(bytes, 0, bytes.length);
			}
			_socket.writeMultiByte("Hello Java啊啊", "GBK");
			
			_socket.flush();
		}
		
		private function onError(e:Event):void
		{
			trace(e.type + ", " + e.toString());
		}
		
		private function onProgress(e:ProgressEvent):void
		{
			//setTimeout(deal, 0.00000000000000000000000000000000000000000000000000001);
			deal();
		}
		
		private function deal():void
		{
			var index:int = -1;
			if (_socket.bytesAvailable >= 30)
			{
				//dealString();
				index = _socket.readUnsignedInt();
				ondeal(index);
			}
		}
		
		private function ondeal(index:int):void
		{
			trace("Send to client --> " + index);
			var cmd:int = _socket.readUnsignedInt();
			trace("cmd -----> " + cmd);
			switch(index)
			{
				case 1:
					break;
				case 2:
					var len:int = _socket.bytesAvailable;
					len = _socket.readUnsignedInt();
					trace("服务器发的包的长度-- > " + len);
					var bytes:ByteArray = getByteArray();
					_socket.readBytes(bytes, 0, len);
					var str:String = bytes.readMultiByte(len, "GBK");
					bytes.position = 0;
					trace(str);
					break;
			}
		}
		
		private function dealString():void
		{
			var i:int = _socket.bytesAvailable;
			var str:String = "";
			while (i-- > 0)
			{
				var value:int;
				
				value = _socket.readByte();
				if (value != 0)
				{
					str += String.fromCharCode(value);
				}
			}
			trace(str);
		}
		
		private function getByteArray():ByteArray
		{
			var byteArray:ByteArray = new ByteArray();
			byteArray.endian = Endian.LITTLE_ENDIAN;
			byteArray.position = 0;
			return byteArray;
		}
		
		public function get socket():Socket 
		{
			return _socket;
		}
		
		static public function get instance():connection 
		{
			if (_instance == null)
			{
				_instance = new connection();
			}
			return _instance;
		}
	}
}